﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Yto.Common.Reflection
{
    public static class ReflectHelper
    {
        public static bool IsAssignableFrom(Type type, Type baseType)
        {
            if(type.IsGenericType && type.GetGenericTypeDefinition() == baseType)
            {
                return true;
            }

            foreach( var interfaceType in type.GetInterfaces())
            {
                if(interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == baseType)
                {
                    return true;
                }
            }
            if(type.BaseType != null)
            {
                return IsAssignableFrom(type.BaseType, baseType);
            }

            return false;
        }

        public static bool IsAsynchronize(this MethodInfo method)
        {
            return method.ReturnType == typeof(Task) ||
                (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>));
        }
    }
}
